/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.rdb.handler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;
import com.sys.midware.rdb.datasource.BDataSource;
import com.sys.midware.rdb.exception.RDBException;

/**
 * 实现HBase表操作的类 <b>ClassName：</b> HBaseDBHandler <br/>
 * <b>@author：</b> mobing <br/>
 * <b>@date：</b> 2015年10月28日 下午2:59:30 <br/>
 * <b>@version: </b> <br/>
 */
public class RDBHandlerMyBatis implements IRDBHandlerMyBatis {

    private static Logger logger = LoggerFactory.getLogger(RDBHandlerMyBatis.class);

    
    private BDataSource dataSource;
    private String mybatisConfig;
    public RDBHandlerMyBatis(BDataSource dataSource) {
      this.dataSource = dataSource;
  }
    public RDBHandlerMyBatis(BDataSource dataSource,String mybatisConfig) {
        this.dataSource = dataSource;
        this.mybatisConfig = mybatisConfig;
    }
    
    private SqlSession openSqlSession(){
        Connection connection1 =null;
        try {
             connection1 =  dataSource.getConnection();
        } catch (SQLException e) {
            
            logger.error("get connection error",e);            
        }
        
        return MyBatisUtil.getSession(connection1,mybatisConfig);
        
        
//        return MyBatisUtil.getSession(connection1);
    }

    @Override
    public <E extends Object> void performInsert(String sql, E record) throws RDBException {
        SqlSession sqlSession =null;
        try {
             sqlSession =  openSqlSession();
            sqlSession.insert(sql, record);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            logger.error("insert error!", e);
            throw new RDBException("RDB performInsert fail:" + sql, e);
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public <E extends Object> E performQuery(String sql,Object obj) throws RDBException {

        SqlSession sqlSession =null;
        try {
             sqlSession =  openSqlSession();
            return sqlSession.selectOne(sql, obj);
        } catch (Exception e) {
            logger.error("insert error!", e);
            throw new RDBException("RDB performQuery fail:" + sql, e);
        } finally {
            sqlSession.close();
        }
    }


    @Override
    public <E extends Object> void performInsertBatch(String sql, Collection<E> record) throws RDBException {

        SqlSession sqlSession =null;
        try {
             sqlSession =  openSqlSession();
            for(E e : record){
                sqlSession.insert(sql, e);
            }
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            logger.error("insert error!", e);
            throw new RDBException("RDB performBatchInsert fail:" + sql, e);
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public <E extends Object> void performUpdate(String sql, E record) throws RDBException {

        SqlSession sqlSession =null;
        try {
             sqlSession =  openSqlSession();
            sqlSession.update(sql, record);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            logger.error("insert error!", e);
            throw new RDBException("RDB performInsert fail:" + sql, e);
        } finally {
            sqlSession.close();
        }

    }

    @Override
    public <E extends Object> void performUpdateBatch(String sql, Collection<E> record) throws RDBException {

        SqlSession sqlSession =null;
        try {
             sqlSession =  openSqlSession();
            for(E e : record){
                sqlSession.update(sql, e);
            }
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            logger.error("insert error!", e);
            throw new RDBException("RDB performBatchUpdate fail:" + sql, e);
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public <E extends Object> List<E> performQuery4List(String sql,Object obj) throws RDBException {
        SqlSession sqlSession =null;
        try {
             sqlSession =  openSqlSession();
            return  sqlSession.selectList(sql, obj);
        } catch (Exception e) {
            logger.error("performQuery4List error!", e);
            throw new RDBException("RDB performQuery4List fail:" + sql, e);
        } finally {
            sqlSession.close();
        }
    }
    

    
//    @Override
//    public <E extends Object> List<E> performQuery4List(String sql,Map<String,Object> obj) throws RDBException {
//        SqlSession sqlSession =null;
//        try {
//             sqlSession =  openSqlSession();
//            return  sqlSession.selectList(sql, obj);
//        } catch (Exception e) {
//            logger.error("performQuery4List error!", e);
//            throw new RDBException("RDB performQuery4List fail:" + sql, e);
//        } finally {
//            sqlSession.close();
//        }
//    }

    @Override
    public <E extends Object> void performDelete(String sql, E obj) throws RDBException {

        SqlSession sqlSession =null;
        try {
             sqlSession =  openSqlSession();
            sqlSession.delete(sql, obj);
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            e.printStackTrace();
            logger.error("insert error!", e);
            throw new RDBException("RDB performInsert fail:" + sql, e);
        } finally {
            sqlSession.close();
        }
    }

    @Override
    public <E extends Object> void performDeleteBatch(String sql, Collection<E> obj) throws RDBException {
        SqlSession sqlSession =null;
        try {
             sqlSession =  openSqlSession();
            for(E e : obj){
                sqlSession.delete(sql, e);
            }
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            logger.error("performDeleteBatch error!"+ sql, e);
            throw new RDBException("RDB performDeleteBatch fail:" + sql, e);
        } finally {
            sqlSession.close();
        }        
    }

    @Override
    public void close() {
        
        
    }

}
